package com.itheima.health.controller.user;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.dto.VeiflcationcodeParam;
import com.itheima.health.result.Result;
import com.itheima.health.service.SendService;
import com.itheima.health.util.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mobile")
@Slf4j
public class SendController {
    @Autowired
    private SendService sendService;
    /*
    * 短信验证
    * */
    @PostMapping("/validateCode/send")
    public Result send(VeiflcationcodeParam veiflcationcodeParam){
        sendService.send(veiflcationcodeParam);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
    /*
    * 登录
    * */
    @PostMapping("/login/smsLogin")
    public Result longin(@RequestBody SmsLoginParam smsLoginParam){
    VeiflcationcodeParam v =sendService.longin(smsLoginParam);
    if (v == null){
        //用户不存在
        return new Result(false,MessageConst.LOGIN_FAIL);
    }

        if (!smsLoginParam.getValidateCode().equals(v.getValidateCode())){
            //验证码有误
            return new Result(false,MessageConst.VALIDATECODE_ERROR);
        }
        //登录成功
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }
}
